package com.zhuss.outer.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.outer.outer.CreateOuterAppendBo;
import com.zhuss.bean.bo.outer.outer.wechat.CollectOuterBo;
import com.zhuss.bean.vo.outer.outer.DetailOuterAppendVo;
import com.zhuss.bean.vo.outer.outer.DetailOuterVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 收藏外包消息 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("outerCollect")
public class OuterCollect extends AbstractProcessor<Object, CollectOuterBo> implements Processor<Object, CollectOuterBo> {

    @Autowired
	@Qualifier("outerRead")
	private OuterRead outerRead;
    
    @Autowired
	@Qualifier("outerAppendRead")
	private OuterAppendRead outerAppendRead;
    
    @Autowired
	@Qualifier("outerAppendCreate")
	private Processor<Object, CreateOuterAppendBo> outerAppendCreate;
    
    /**
     * 校验
     */
    @Override
    public void check(CollectOuterBo bo) throws WpsException {
    	Long outerId = bo.getOuterId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailOuterAppendVo result = outerAppendRead.getByType(appendType, outerId, bo.getConcurrentUserId());
    	
    	if (result != null) {
    		throw new WpsException(ExceptionCode.外包消息已收藏);
    	}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CollectOuterBo param) throws WpsException {
    	
    	Long outerId = param.getOuterId();
    	DetailOuterVo outer = ((DetailOuterVo) outerRead.detail(outerId));
    	String name = outer.getTitle();
    	
    	CreateOuterAppendBo bo = new CreateOuterAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	BeanUtils.copyProperties(outer, bo);
    	bo.setOuterId(outerId);
    	bo.setType(param.getAppendType());
    	bo.setTitle(outer.getTitle());
    	bo.setRequirement(outer.getRequirement());
    	bo.setPhone(outer.getPhone());
    	bo.setPic(outer.getPic());
    	outerAppendCreate.process(bo);
    	
    	
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CollectOuterBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.外包消息.key());
    	record.setAction(LogActionEnum.外包消息收藏.key());
    	record.setTargetId(param.getOuterId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
